package com.chenliang.controller;

import com.chenliang.domain.Role;
import com.chenliang.domain.UserInfo;
import com.chenliang.service.IUserInfoService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.controller
 * 作用：
 */
@Controller
@RequestMapping("/UserInfo")
public class UserInfoController {
    @Autowired
    private IUserInfoService service;

    @RequestMapping("/findAll")
    public String findAll(Model model,
       @RequestParam(name = "page", defaultValue = "1", required = true) Integer page,
       @RequestParam(name = "pageSize", defaultValue = "3", required = true) Integer pageSize) {

        List<UserInfo> userInfos = service.findall(page, pageSize);
        PageInfo pageInfo = new PageInfo(userInfos);
        model.addAttribute("pageInfo", pageInfo);
        return "user-list";

    }
//添加用户
    @RequestMapping("/addUser")
    public String addUserInfo(UserInfo userInfo){
        service.addUser(userInfo);

        return "redirect:/UserInfo/findAll";

    }
//    用户详情
    @RequestMapping("/findById")
    public ModelAndView findById(String id){
        UserInfo userInfo= service.findById(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("userInfo",userInfo);
        mv.setViewName("user-show");
        return mv;
    }


//    用户添加角色(1.查该用户还没有的权限。添加权限)
    @RequestMapping("/userAddRole")
    public String userAddRole(Model model,@RequestParam(name = "id")String id){
        List<Role> roles= service.userAddRole(id);
        model.addAttribute("roles",roles);
        model.addAttribute("userId",id);
        return "user-role-add";

    }
    @RequestMapping("/userAddRoleAll")
    public String userAddRoleAll(Model model,@RequestParam(name="userId",required = true)String userId,@RequestParam(name="ids" )String[] ids){
        if (ids!=null||ids.length>0){
//            有添加
service.addRole(userId,ids);

        }
        UserInfo userInfo = service.findById(userId);
       model.addAttribute("userInfo",userInfo);
        return "user-show";
    }
}
